package org.dce.screencapturer.editors;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;

import org.dce.screencapturer.objects.FillAttributes;
import org.dce.screencapturer.objects.ShapeObject;

@SuppressWarnings("serial")
public class FillEditor extends Editor {
	
	private JColorChooser chooser = new JColorChooser();
	private JCheckBox enableFill = new JCheckBox("Fill?");
	private JSlider slider = new JSlider(JSlider.HORIZONTAL,0,100,0);
	public FillEditor(ShapeObject editedObject){
		super(editedObject,"Edit paint");
		
		JPanel panel = new JPanel(new BorderLayout());
		
		JPanel topPanel = new JPanel(new GridBagLayout());
		topPanel.add(enableFill,new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
		topPanel.add(new JLabel("Transparency"),new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0, 0));
		topPanel.add(slider,new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
		
		panel.add(topPanel,BorderLayout.NORTH);
		JTabbedPane tabs = new JTabbedPane();
		tabs.addTab("Color", chooser);
		tabs.addTab("Gradient", createGradientPanel());
		panel.add(tabs,BorderLayout.CENTER);
		
		add(panel,BorderLayout.CENTER);

	}

	private Component createGradientPanel() {
		return new JPanel();
	}

	@Override
	protected void readFromObject() {
		
		FillAttributes fillAttributes = editedObject.getFillAttributes();
		
		boolean enabled = fillAttributes.isEnabled();
		
		enableFill.setSelected(enabled);
		
		if (enabled){
			slider.setValue(fillAttributes.getTransparency());
			chooser.setColor(fillAttributes.getColor());
		}
		
	}

	@Override
	protected void writeToObject() {
		FillAttributes fillAttributes = (FillAttributes)editedObject.getFillAttributes().clone();
		fillAttributes.setEnabled(enableFill.isSelected());
		fillAttributes.setColor(chooser.getColor());
		fillAttributes.setTransparency(slider.getValue());
		editedObject.setFillAttributes(fillAttributes);
	}
}
